<?php
/*****************************************************************************
**	Класс:	Работа с MySQL													**
**	Автор:	Dark Dayver														**
**	Версия:	1.0b															**
**	Дата:	25/12-2009														**
*****************************************************************************/
if (IN_P_ENIGMA)
{
	die('HACK!');
}

class db
{
	var $link;	// Ссылка на подключенную базу данных
	var $txt_query; // Сформированный запрос
	var $res_query; // Ссылка на полученный результат
	var $result = false; // Результат выполнения запроса
	var $aff_rows = false; // Количество вставленных строк
	var $insert_id = false; // ID последней вставленной через INSERT строки
	var $error; // Номер и текст ошибки

	function db($dbhost, $dbuser, $dbpass)
	{
		$this->link = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connect DB: ' . mysql_error());
		@mysql_query("SET CHARSET utf8", $this->link);
	}

	function query()
	{
		$this->aff_rows = false;
		$this->res_query = @mysql_query($this->txt_query, $this->link);
		if ($this->res_query === false)
		{
			$this->error = mysql_errno($this->link) . ': ' . mysql_error($this->link);
			return false;
		}
		else
		{
			$this->aff_rows = @mysql_affected_rows($this->link);
			$this->insert_id = @mysql_insert_id($this->link);
			return true;
		}
	}

	function select($select, $from_db, $from_tbl)
	{
		$this->txt_query = 'SELECT ';
		if(!is_array($select))
		{
			$select = array($select);
		}
		$selects = '';
		foreach ($select as $tmp)
		{
			if ($tmp == '*' || $tmp[0] == '`' || $tmp[strlen($tmp) - 1] == '`')
			{
				$selects .= ', ' . $tmp;
			}
			else
			{
				$selects .= ', `' . $tmp . '`';
			}
		}
		$selects = substr($selects, 2);
		$this->txt_query .= $selects . ' FROM `' . $from_db . '`.`' . $from_tbl . '`';
	}

	function delete($from_db, $from_tbl)
	{
		$this->txt_query = 'DELETE FROM `' . $from_db . '`.`' . $from_tbl . '`';
	}

	function update($update, $to_db, $to_tbl)
	{
		$this->txt_query = 'UPDATE `' . $to_db . '`.`' . $to_tbl . '` SET ';
		$updates = '';
		foreach ($update as $key=>$value)
		{
			if ($value === NULL)
			{
				$updates .= ', `' . $key . '` = NULL';
			}
			else
			{
				$updates .= ', `' . $key . "` = '" . $value . "'";
			}
		}
		$this->txt_query .= substr($updates, 2);
	}

	function insert ($insert, $to_db, $to_tbl, $type = false)
	{
		if ($type == 'ignore')
		{
			$this->txt_query = 'INSERT IGNORE INTO ';
		}
		else if ($type == 'replace')
		{
			$this->txt_query = 'REPLACE INTO ';
		}
		else
		{
			$this->txt_query = 'INSERT INTO ';
		}
		$keys = '';
		$values = '';
		foreach ($insert as $key=>$value)
		{
			$keys .= ', `' . $key . '`';
			$values .= ", '" . $value . "'";
		}
		$this->txt_query .= '`' . $to_db . '`.`' . $to_tbl . '` (' . substr($keys, 2) . ') VALUES (' . substr($values, 2) . ')';
	}

	function where($where)
	{
		if (strlen($this->txt_query) > 0)
		{
			$this->txt_query .= ' WHERE ' . $where;
		}
	}

	function order($order, $sort = false)
	{
		if (strlen($this->txt_query) > 0)
		{
			$this->txt_query .= ' ORDER BY `' . $order . '`';
			if ($sort == 'down')
			{
				$this->txt_query .= ' DESC';
			}
			else if ($sort == 'up')
			{
				$this->txt_query .= ' ASC';
			}
		}
	}

	function group($group, $sort = false)
	{
		if (strlen($this->txt_query) > 0)
		{
			$this->txt_query .= ' GROUP BY `' . $group . '`';
			if ($sort == 'down')
			{
				$this->txt_query .= ' DESC';
			}
			else if ($sort == 'up')
			{
				$this->txt_query .= ' ASC';
			}
		}
	}

	function limit($limit, $start = 0)
	{
		if (strlen($this->txt_query) > 0 && $limit > 0)
		{
			$this->txt_query .= ' LIMIT ' . $start . ', ' . $limit;
		}
	}

	function res_row()
	{
		if ($this->res_query)
		{
			$this->result = @mysql_fetch_assoc($this->res_query);
		}
		else
		{
			$this->result = false;
		}
		return $this->result;
	}

	function res_arr()
	{
		if ($this->res_query)
		{
			$i = 0;
			$this->result = array();
			while ($tmp = @mysql_fetch_assoc($this->res_query))
			{
				$this->result[$i] = $tmp;
				$i++;
			}
			if ($i = 0)
			{
				$this->result = false;
			}
		}
		else
		{
			$this->result = false;
		}
		return $this->result;
	}

	function clean()
	{
		$this->txt_query = '';
		$this->res_query = false;
		$this->result = false;
		$this->aff_rows = false;
		$this->insert_id = false;
		$this->error = '';
	}
}
?>
